<template>
	<view class="main">
		<view class="item">
			<view class="tit">
				退货金额
			</view>
			<u--input v-model="money" fontSize="15px" placeholder="请输入退货金额" border="bottom" clearable></u--input>
			<!-- <view class="msg">
				退货規則
			</view> -->
		</view>

		<view class="item">
			<view class="tit">
				商品图片上传
			</view>
			<u--image :showLoading="true" :src="seleUrl" width="80px" height="80px" @click="upload"></u--image>
		</view>

		<view class="btn" @tap="showmodel">
			提交
		</view>
		<view class="list">
			<view class="tit">
				退货记录
			</view>
			<u-empty v-if="list.length == 0" text="暂无记录" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
			</u-empty>
			<view class="items" v-for="(item,index) in list" :key="index">
				<view class="left">
					<text>{{item.status_text}}</text>
					<text>{{item.createtime}}</text>
				</view>
				<view class="right">
					<text v-if="item.record_type == 1">+</text>
					<text v-if="item.record_type == 0">-</text>
					{{item.money}}
					<button v-if="item.status == 1" class="confirm-btn" @tap="handleConfirmWd(item)"
						:disabled="item.disabled">
						确认提现
					</button>
				</view>
			</view>
		</view>
		<u-modal confirmColor="#000" :show="show" :title="title" @confirm="yanzheng" :closeOnClickOverlay="true"
			@close="show = false">
			<view class="slot-content">
				<input v-model="value" placeholder="请输入二级密码" type="password" value="" />
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		cousout,
		checkpwd2,
		user,
		withdrawlistsx,
		confirmWd
	} from '@/api/index.js'
	export default {
		data() {
			return {
				seleUrl: '/static/img/shang.png',
				money: '',
				image: '',
				show: false,
				value: '',
				info: {},
				isBtn: false,
				title: '验证二级密码',
				list: [],
				loading: false, // 新增加载状态
				noMore: false, // 新增无更多数据标志
			}
		},
		onReachBottom() {
			if (this.page == this.last_page) {
				uni.showToast({
					title: '已经是最后一页了',
					icon: 'none'
				})
			} else {
				this.page++
				this.getList()
			}
		},
		onLoad() {
			this.getInfo()
			this.getList()
		},
		onShow() {
			this.getInfo();
			this.getList();
		},
		methods: {
			async handleConfirmWd(item) {
				try {
					// 禁用按钮，防止重复点击
					item.disabled = true;

					// 调用确认提现接口
					const res = await confirmWd({
						id: item.id // 假设 item.id 是提现记录的唯一标识
					});

					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
					console.log(res)
					if (res.code === 1) {
						let data = res.data
						// console.log(res.data)
						this.tixian(data)
					}
				} catch (error) {
					console.error('确认提现失败:', error);
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none'
					});
				} finally {
					// 无论成功或失败，重新启用按钮
					item.disabled = false;
				}
			},

			async tixian(data) {
				if (uni.canIUse('requestMerchantTransfer')) {
					uni.requestMerchantTransfer({
						mchId: data.mchId,
						appId: data.appId,
						package: data.package_info,
						success: (res) => {
							// res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
							console.log('success:', res);
							uni.redirectTo({
								url: '/pageB/tixian'
							})
						},
						fail: (res) => {
							console.log('fail:', res);
						},
					});
				} else {
					uni.showModal({
						content: '你的微信版本过低，请更新至最新版本。',
						showCancel: false,
					});
				}
			},
			async getList() {
				if (this.loading || this.noMore) return;

				this.loading = true;
				try {
					let res = await withdrawlistsx({
						page_size: '15',
						moneytype: '7',
					});

					console.log(res);

					// 直接替换原有数据，而不是追加
					this.list = res.data.data || [];

					// 标记没有更多数据
					this.noMore = true;

				} catch (error) {
					console.error('获取退货记录失败:', error);
					uni.showToast({
						title: '获取数据失败',
						icon: 'none'
					});
				} finally {
					this.loading = false;
				}
			},
			async getInfo() {
				let res = await user()
				this.info = res.data
			},
			showmodel() {
				if (this.info.is_password2 == 0) {
					uni.showToast({
						title: '请先设置二级密码',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pageB/psd2'
						})
					}, 1000)
				} else {
					this.show = true
				}
			},
			async yanzheng() {
				if (this.value == '') {
					uni.showToast({
						title: '请输入二级密码',
						icon: 'none'
					})
					return
				}
				let res = await checkpwd2({
					password: this.value
				})
				if (res.code == 1) {
					uni.showToast({
						title: '验证成功',
						icon: 'none'
					})
					this.show = false
					if (!this.isBtn) {
						this.confirm()
					}
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1200)
				}
			},
			upload() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res)
						// this.seleUrl = res.tempFilePaths[0]
						this.uploadFilePromise(res.tempFilePaths[0])
					}
				})
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://www.zpyd.top/api/common/upload',
						filePath: url,
						name: 'file',
						header: {
							'token': uni.getStorageSync('token') //自定义请求头信息 
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							// console.log(res)
							let ndata = JSON.parse(res.data)
							// console.log(ndata)
							this.seleUrl = ndata.data.fullurl
							this.image = ndata.data.url
							uni.showToast({
								title: '上传成功',
								icon: 'none'
							})
						}
					});
				})
			},
			async confirm() {
				this.isBtn = true
				if (!this.money) {
					uni.showToast({
						title: '请输入退货金额',
						icon: 'none'
					})
					return;
				}
				if (!this.image) {
					uni.showToast({
						title: '请上传商品图片',
						icon: 'none'
					})
					return;
				}
				let res = await cousout({
					money: this.money,
					image: this.image
				})

				uni.showToast({
					title: res.msg,
					icon: 'none'
				})

				setTimeout(() => {
					this.isBtn = false
				}, 2000)
				if (res.code == 1) {
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		background-color: #f6f6f6;
		padding: 18px 15px;
		min-height: 100vh;

		.slot-content {
			width: 100%;

			input {
				width: 100%;
			}
		}

		.item {
			padding: 20px;
			background-color: #fff;
			border-radius: 15px;
			margin-bottom: 15px;

			.tit {
				font-size: 30rpx;
				color: #000;
				font-weight: 700;
				margin-bottom: 20px;
			}

			::v-deep.u-input {
				// padding-bottom: 16px !important;
				margin-bottom: 20px;
				color: #000;
			}

			::v-deep.uni-input-input {
				color: #000;
			}

			.msg {
				font-size: 12px;
				color: #333;
			}

		}

		.confirm-btn {
			margin-left: 10px;
			padding: 4px 8px;
			background-color: #EA504F;
			color: white;
			border-radius: 4px;
			font-size: 12px;

			&:disabled {
				background-color: #ccc;
			}
		}

		.modal-content {
			padding: 20px;
			text-align: center;
			font-size: 16px;
		}

		.btn {
			width: 100%;
			padding: 18px 0;
			text-align: center;
			margin-bottom: 15px;
			border-radius: 5px;
			background-color: #EA504F;
			color: #fff;
		}

		.list {
			position: relative;
			z-index: 10;
			flex: 1;
			padding: 40rpx;
			background-color: #fff;
			border-radius: 30rpx 30rpx 0 0;

			.tit {
				color: #ea504f;
				font-size: 32rpx;
				font-weight: 500;
				margin-bottom: 30rpx;
			}

			.items {
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #000;
				// margin-bottom: 20rpx;
				padding: 24rpx 0;
				border-bottom: 1rpx solid #ddd;
				box-sizing: border-box;

				.left {
					display: flex;
					flex-direction: column;
					color: #000;
					// font-weight: 700;

					text:first-child {
						font-size: 32rpx;
						margin-bottom: 20rpx;
					}

					text:last-child {
						font-size: 26rpx;
						color: #666;
					}
				}

				.right {
					font-weight: 700;
				}
			}
		}
	}
</style>